package com.thinkgsp.jxc.model;

import java.math.BigDecimal;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
/**
 * 客户状态类,判断客户的对象是否符合交易条件
 * @author evan
 *
 */
public class CustomerState extends BaseModel {

	private static final long serialVersionUID = -5829609207574087118L;
	private Integer customerId;
	private boolean enable = true;
	private boolean acctLocked;
	private boolean acctExpired;
	private boolean acctEnabled;
	private boolean deleted;
	private Integer creditAmount;
	private Integer creditDay;
	private BigDecimal arpAmount;
	private Integer remainDay;
	//private String description;
	
	public CustomerState() {
		
	}
	
	public CustomerState(Customer cust) {
		customerId = cust.getCustomerId();
		this.setAcctLocked(cust.getAcctLocked());
		this.setAcctExpired(cust.getAcctExpired());
		this.setDeleted(cust.getDeleted());
		this.creditAmount = cust.getCreditAmount();
		this.creditDay = cust.getCreditDay();
	}
	/**
	 * 客户ID
	 * @return the customerId
	 */
	public Integer getCustomerId() {
		return customerId;
	}

	/**
	 * @param customerId the customerId to set
	 */
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	/**
	 * 客户状态，是否可以进行交易
	 * @return the enable
	 */
	public boolean isEnable() {
		return enable;
	}

	/**
	 * @param enable the enable to set
	 */
	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	/**
	 * 锁定状态
	 * @return the acctLocked
	 */
	public boolean isAcctLocked() {
		return acctLocked;
	}

	/**
	 *
	 * @param acctLocked the acctLocked to set
	 */
	public void setAcctLocked(boolean acctLocked) {
		this.acctLocked = acctLocked;
		if(acctLocked) {
			enable = false;
		}
	}

	/**
	 * 过期状态
	 * @return the acctExpired
	 */
	public boolean isAcctExpired() {
		return acctExpired;
	}

	/**
	 * @param acctExpired the acctExpired to set
	 */
	public void setAcctExpired(boolean acctExpired) {
		this.acctExpired = acctExpired;
		if(acctExpired) {
			enable = false;
		}
	}

	/**
	 * 账户启用
	 * @return the acctEnabled
	 */
	public boolean isAcctEnabled() {
		return acctEnabled;
	}

	/**
	 * @param acctEnabled the acctEnabled to set
	 */
	public void setAcctEnabled(boolean acctEnabled) {
		this.acctEnabled = acctEnabled;
		if(!acctEnabled) {
			enable = false;
		}
	}

	/**
	 * 是否已删除
	 * @return the deleted
	 */
	public boolean isDeleted() {
		return deleted;
	}

	/**
	 * @param deleted the deleted to set
	 */
	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
		if(deleted) {
			enable = false;
		}
	}

	/**
	 * 信用额度
	 * @return the creditAmount
	 */
	public Integer getCreditAmount() {
		return creditAmount;
	}

	/**
	 * @param creditAmount the creditAmount to set
	 */
	public void setCreditAmount(Integer creditAmount) {
		this.creditAmount = creditAmount;
	}

	/**
	 * 信用天数
	 * @return the creditDay
	 */
	public Integer getCreditDay() {
		return creditDay;
	}

	/**
	 * @param creditDay the creditDay to set
	 */
	public void setCreditDay(Integer creditDay) {
		this.creditDay = creditDay;
	}

	/**
	 * 欠款金额
	 * @return the arpAmount
	 */
	public BigDecimal getArpAmount() {
		return arpAmount;
	}

	/**
	 * @param arpAmount the arpAmount to set
	 */
	public void setArpAmount(BigDecimal arpAmount) {
		this.arpAmount = arpAmount;
		if(this.creditAmount == 0) {
			return;
		}
		if(arpAmount.compareTo(new BigDecimal(this.creditAmount)) >= 0) {
			this.acctExpired = true;
			this.enable = false;
		}
	}

	/**
	 * 剩余天数
	 * @return the remainDay
	 */
	public Integer getRemainDay() {
		return remainDay;
	}

	/**
	 * @param remainDay the remainDay to set
	 */
	public void setRemainDay(Integer remainDay) {
		this.remainDay = remainDay;
		if(creditDay == 0) {
			return;
		}
		if(remainDay <= 0) {
			this.acctExpired = true;
			this.enable = false;
		}
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		StringBuilder sb = new StringBuilder();
		if(enable) {
			sb.append("状态正常，可进行交易。");
		} else {
			sb.append("禁止交易。原因:");
			if(acctLocked) {
				sb.append("账户已经锁定;");
			}
			if(acctExpired) {
				sb.append("账户已经过期;");
			}
			if(!acctEnabled) {
				sb.append("账户已经禁用;");
			}
			if(deleted) {
				sb.append("账户已经删除;");
			}
			if(remainDay <= 0) {
				sb.append("账户的欠款天数已经到达信用天数;");
			}
			if(arpAmount != null && arpAmount.compareTo(new BigDecimal(creditAmount)) >= 0) {
				sb.append("账户的欠款金额已经超出信用额度;");
			}
		}
		return sb.toString();
	}

	@Override
	public String toString() {
		ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE);
		sb.append("customerId",this.customerId)
		  .append("enable",this.enable)
		  .append("description",this.getDescription());
		
		return sb.toString();
	}

	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}
		if(!(o instanceof CustomerState)) {
			return false;
		}
		final CustomerState cust = (CustomerState)o;
		if(customerId != null) {
			return customerId.equals(cust.getCustomerId());
		} else {
			return cust.getCustomerId() == null;
		}
	}

	@Override
	public int hashCode() {
		return (this.customerId != null ? this.customerId.hashCode() : 0);
	}

}
